package cn.lg.soar.system.biz.modules.auth.client;

import cn.lg.soar.core.annotations.ApiClient;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.model.PostDTO;
import cn.lg.soar.system.api.model.UserPostItemVO;
import cn.lg.soar.system.api.service.IPostApi;
import cn.lg.soar.system.biz.modules.auth.entity.Post;
import cn.lg.soar.system.biz.modules.auth.service.IPostService;
import cn.lg.soar.system.biz.modules.auth.service.UserPostRelService;
import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author luguoxiang
 * @date 2022/1/3
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Hidden
@ApiClient("boot")
@RestController
@RequestMapping(ModuleConst.CLIENT_PATH + "/post")
public class PostApiClient implements IPostApi {

    @Autowired
    private IPostService service;
    @Autowired
    private UserPostRelService userOrgService;

    @GetMapping("getById")
    @Override
    public PostDTO getById(@RequestParam(name = "id") Long id) {
        return toDto(service.getById(id));
    }

    @GetMapping("listByIds")
    @Override
    public List<PostDTO> listByIds(@RequestParam(name = "ids") List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return service.listByIds(ids).stream().map(this::toDto).collect(Collectors.toList());
    }

    @GetMapping("idsByUserId")
    @Override
    public List<Long> idsByUserId(@RequestParam(name = "userId") Long userId) {
        return userOrgService.getPostIds(userId);
    }

    @GetMapping("listByUserId")
    @Override
    public List<UserPostItemVO> listByUserId(@RequestParam("userId") Long userId) {
        return service.listByUserId(userId);
    }

    @GetMapping("getByUserId")
    @Override
    public UserPostItemVO getByUserId(Long userId) {
        return service.getByUserId(userId);
    }

    @GetMapping("idsByOrgAndTag")
    @Override
    public List<Long> idsByOrgAndTag(@RequestParam("orgId") Long orgId, @RequestParam("tagCode") String tagCode) {
        return service.idsByOrgAndTag(orgId, tagCode);
    }

    private PostDTO toDto(Post entity) {
        PostDTO dto = new PostDTO();
        dto.setId(entity.getId());
        dto.setName(entity.getName());
        dto.setAlias(entity.getAlias());
        dto.setRemark(entity.getRemark());
        dto.setOrgId(entity.getOrgId());
        dto.setDisabled(entity.getDisabled());
        return dto;
    }
}
